package com.xingdata.jjxc.activity.fragment;

import jar.listener.CaptureListener;
import jar.observer.CommunicationObserver;
import jar.tools.UdpTools;

import java.io.File;
import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.HudVideoAdapter;
import com.xingdata.jjxc.callback.DilalogListener;
import com.xingdata.jjxc.enty.CarcorderEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.Consumer;
import com.xingdata.jjxc.utils.ImageUtils;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.VideoTools;
import com.xingdata.jjxc.views.MyAlertDialog;

//循环视频
public class HudVideoFragment extends Fragment implements OnItemClickListener,
		CaptureListener {
	private GridView circulation;
	private HudVideoAdapter adapter;
	private ArrayList<CarcorderEntity> carcorderEntities = new ArrayList<CarcorderEntity>();
	private String videoUrl = SDCardTools.getFilePath("video") + "/1.mp4";
	private String TAG = "HudVieoFragment";
	private ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
	private Consumer consumer=new Consumer();
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
		CommunicationObserver.addListener(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		CarcorderEntity carcorderEntity = null;
		// for (int i = 0; i < 10; i++) {
		// carcorderEntity = new CarcorderEntity();
		// carcorderEntity.setCrtTime(StringUtil.ToStringTime(new Date(System
		// .currentTimeMillis())));
		// carcorderEntity.setPlayTime("19:00:00");
		//
		// carcorderEntity.setVideoSize("10M");
		// carcorderEntity.setVideoTitle("这是第" + i + "个视频");
		// carcorderEntity.setVideoUrl("");
		// carcorderEntities.add(carcorderEntity);

		doPost_getCarcorders();
		// }
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.circulationfragment, container,
				false);
		circulation = (GridView) view.findViewById(R.id.circulation);
		circulationview();
		return view;
	}

	private void circulationview() {
		adapter = new HudVideoAdapter(getActivity(), carcorderEntities,imageViews,consumer);
		circulation.setAdapter(adapter);
		circulation.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, final int postion,
			long arg3) {
		// Bundle bundle = new Bundle();
		// bundle.putString("url", carcorderEntities.get(arg2).getHud_url());
		// Intent intent = new Intent(getActivity(), VideoUploadActivity.class);
		// intent.putExtras(bundle);
		// getActivity().startActivity(intent);

		ArrayList<String> titleList = new ArrayList<String>();
		titleList.add("");
		titleList.add("播放");
		titleList.add("下载");
		new MyAlertDialog(getActivity(), titleList, new DilalogListener() {
			@Override
			public void onSure() {
				VideoTools.palyVideo(getActivity(),
						carcorderEntities.get(postion).getHud_url());
			}

			@Override
			public void onCancel() {
				String localUrl = SDCardTools
						.getFilePath(SDCardTools.VIDEO_NAME)
						+ "/"
						+ SDCardTools.getFileName(carcorderEntities
								.get(postion).getfName());
				if (new File(localUrl).exists()) {
					showToast("已经存在");
					return;
				}
				showToast("正在下载");
				HttpUtil.getInstance(getActivity()).downloadFile(
						carcorderEntities.get(postion).getHud_url(), localUrl,
						new HttpResponseListener() {

							@Override
							public void onSuccess(RespEntity respEntity) {
								showToast("下载完毕");
							}

							@Override
							public void onLoading(int vurrent) {

							}

							@Override
							public void onFailure(String result) {
								// TODO Auto-generated method stub

							}
						});

			}
		});
	}

	@Override
	public void onCapture(String arg0) {
		doPost_getCarcorders();
	}

	private void doPost_getCarcorders() {
		HttpUtil.getInstance(getActivity()).requestByHttpGet(
				UdpTools.getHUD_VideoUrl(".json"), new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						if (respEntity.getMsg().length()==0||respEntity.getMsg().equals("{}")) {
							return;
						}
						carcorderEntities.clear();
						carcorderEntities
								.addAll((ArrayList<CarcorderEntity>) JSON
										.parseArray(respEntity.getMsg(),
												CarcorderEntity.class));

						for (CarcorderEntity entity : carcorderEntities) {
							entity.setHud_url(UdpTools.getHUD_VideoUrl(entity
									.getfName()));
						}
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFailure(String result) {
						// TODO Auto-generated method stub

					}
				});

	}
	private void showToast(String s){
		Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
	}
	

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		consumer.cancleAll();
		for (ImageView view : imageViews) {
			ImageUtils.releaseImageViewResouce(view);
		}
	}
}
